//
//  MyScoreViewController.m
//  DuBai
//
//  Created by darktm on 14-3-11.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "MyScoreViewController.h"

#import "MyScoreViewCell.h"

#import "GradeLog.h"

#import "TaskWithGetGradeLog.h"

@interface MyScoreViewController ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray* scoreArray;

@end

@implementation MyScoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self loadScores];
}



#pragma mark - tableview delegate -

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 27)];
    view.backgroundColor = [UIColor blackColor];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 107, 27)];
    label1.text = @"日期";
    label1.backgroundColor = [UIColor clearColor];
    label1.textColor = [UIColor brownColor];
    label1.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(107, 0, 107, 27)];
    label2.text = @"事件";
    label2.backgroundColor = [UIColor clearColor];
    label2.textColor = [UIColor brownColor];
    label2.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(214, 0, 107, 27)];
    label3.text = @"积分";
    label3.backgroundColor = [UIColor clearColor];
    label3.textColor = [UIColor brownColor];
    label3.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label3];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 27.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.scoreArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyScoreViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GradeLog *log = self.scoreArray[indexPath.row];
    cell.time.text = log.createtime;
    cell.event.text = log.eventObject;
    cell.score.text = log.grade;
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetGradeLog class]]){
        self.scoreArray = rsult;
        int count = 0;
        
        for (GradeLog *log in self.scoreArray) {
            count += [log.grade intValue];
        }
        self.totalScore.text = [NSString stringWithFormat:@"%d",count];
        
        [self.tableView reloadData];
    }}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}

#pragma mark - customer method -

-(void)loadScores{
    
    if(self.scoreArray){
        [self.scoreArray removeAllObjects];
    }else{
        self.scoreArray = [NSMutableArray array];
    }
    
    TaskWithGetGradeLog *order = [[TaskWithGetGradeLog alloc] init];
    order.delegate = self;
    [order start];
}

@end
